package com.hang.service.impl;

import cn.hutool.core.util.StrUtil;
import com.hang.config.MsmConfig;
import com.hang.config.RedisConfig;
import com.hang.service.IMsmService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
@Slf4j
public class MsmServiceImpl implements IMsmService {
    private MsmConfig msmConfig;

    private RedisConfig redisConfig;
    /**
     * 判断验证码是否存在
     * @param phone
     * @return
     */
    @Override
    public boolean codeExist(String phone) {
        RedisTemplate<String, Object> redisTemplate = redisConfig.getRedisTemplateByDb(0);

        Object codeObj = redisTemplate.opsForValue().get(phone);
        String code = StrUtil.toString(codeObj);

        return StrUtil.isBlank(code);
    }

    @Override
    public boolean send(String phone, String code) {
        if(StrUtil.isBlank(phone)){
            return false;
        }

        //整合榛子云的短信发送服务
        try {
            msmConfig.sendMessage(code,phone);
        }catch (Exception e){
            log.error("error => {}",e.toString());
            return false;
        }
        return true;
    }
}